package com.xiaoshuidi.cloud.module.report.xxljob;

import com.xiaoshuidi.cloud.framework.common.exception.ServiceException;
import com.xiaoshuidi.cloud.framework.tenant.core.aop.TenantIgnore;
import com.xiaoshuidi.cloud.framework.tenant.core.job.TenantJob;
import com.xiaoshuidi.cloud.module.report.constant.CustomTypeEnum;
import com.xiaoshuidi.cloud.module.report.constant.RoomHouseReserveStatus;
import com.xiaoshuidi.cloud.module.report.dal.DO.*;
import com.xiaoshuidi.cloud.module.report.dal.DO.report.ReportRoomReserveDO;
import com.xiaoshuidi.cloud.module.report.dal.mapper.*;
import com.xiaoshuidi.cloud.module.report.dal.mapper.report.ReportRoomReserveMapper;
import com.xiaoshuidi.cloud.module.report.enums.ErrorCodeConstants;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 预租台账job每天6:30
 */
@Component
@Slf4j
public class ReportRoomReserveHandleJob {

    @Resource
    private ReportRoomReserveMapper reportRoomReserveMapper;
    @Resource
    private RoomHouseMapper roomHouseMapper;
    @Resource
    private ApartmentMapper apartmentMapper;
    @Resource
    private FinanceMoneyFlowMapper financeMoneyFlowMapper;


    @XxlJob("reportRoomReserveHandleJob")
    @TenantIgnore
    public void execute() {
        //获取未处理的台账
        long totalNum = reportRoomReserveMapper.countReverseUnHandle();
        if(totalNum<=200){
            //合账数<100直接一次处理
            List<ReportRoomReserveDO> list = reportRoomReserveMapper.selectReverseUnHandle();
            handleLedgerList(list);
        }else {
            //分100 一批次处理
            int pageNo = 0;
            int pageSize = 200;
            List<ReportRoomReserveDO> list ;
            do {
                int from = pageNo*pageSize;
                list = reportRoomReserveMapper.selectReverseUnHandlePage(from,pageSize);
                handleLedgerList(list);
                pageNo++;
            }while (list.size()==200);
        }
    }

    /**
     * 填充相关数据到台账表，方便查询
     * @param list
     */
    private void handleLedgerList(List<ReportRoomReserveDO> list){
        if(CollectionUtils.isEmpty(list)){
            return;
        }
        //门店id 门店名称 房源名称 支付方式
        list.forEach(infoDO->{
            if(StringUtils.isBlank(infoDO.getApartmentName())){
                //拼凑房源名称
                try {
                    RoomHouseDO houseDO = roomHouseMapper.selectById(infoDO.getRoomHouseId());
                    if(houseDO == null){
                        log.error("获取room_house房源信息失败,roomId:{}",infoDO.getRoomHouseId());
                        throw new ServiceException(ErrorCodeConstants.DATA_IS_NOT_FOUND);
                    }
                    infoDO.setApartmentId(houseDO.getApartmentId());
                    ApartmentDO apartmentDO = apartmentMapper.selectById(houseDO.getApartmentId());
                    if(apartmentDO == null){
                        log.error("获取room_apartment房源信息失败,apartmentId:{}",houseDO.getApartmentId());
                        throw new ServiceException(ErrorCodeConstants.DATA_IS_NOT_FOUND);
                    }
                    infoDO.setArea(houseDO.getArea());
                    infoDO.setApartmentName(apartmentDO.getName());
                    infoDO.setCustomerType(CustomTypeEnum.getValueByType(infoDO.getCustomerType()));
                    //集中式和分散式拼凑方法不一样
                    StringBuilder houseName = new StringBuilder(houseDO.getBuildingNo()).append("栋");
                    if(houseDO.getIsConcentrated()){
                        //集中式 小区名-楼栋-房源名称   room_house.area - room_house.building_no栋 - room_house.name
                        infoDO.setHouseResource(houseName.append(houseDO.getName()).toString());
                    }else {
                        //分散式  小区名-楼栋-单元-房源名称 room_house.area - room_house.building_no栋 - room_house.unit_no单元 - room_house.name
                        infoDO.setHouseResource(houseName.append(houseDO.getUnitNo()).append("单元").append(houseDO.getName()).toString());
                    }
                } catch (ServiceException e) {
                    log.error("========处理房源信息失败=====");
                    e.printStackTrace();
                }
            }

            try {
                if(infoDO.getPaymentId()!=null){
                    FinanceMoneyFlowDO moneyFlowDO = financeMoneyFlowMapper.selectById(infoDO.getPaymentId());
                    infoDO.setPayWay(moneyFlowDO.getPayMethod());
                }
                if(RoomHouseReserveStatus.containCode(infoDO.getStatus())){
                    infoDO.setStatus(RoomHouseReserveStatus.getNameByCode(infoDO.getStatus()));
                }
            } catch (Exception e) {
                log.error("=======获取支付信息失败========paymentId:{}",infoDO.getPaymentId());
                e.printStackTrace();
            }
            infoDO.setHandleFlag(1);
        });

        reportRoomReserveMapper.updateBatch(list,list.size());
    }


}
